package look4ss.aplicativo.fragments;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.Marker;
import java.util.ArrayList;
import java.util.Calendar;
import look4ss.aplicativo.ActivityApagar;
import look4ss.aplicativo.ActivityDetalhes;
import look4ss.aplicativo.ActivityEnviar;
import look4ss.aplicativo.ActivityImagem;
import look4ss.aplicativo.ActivityPrincipal;
import look4ss.aplicativo.R;
import look4ss.aplicativo.lista.Pontos;

public class AbaMapa extends Fragment implements OnMapReadyCallback {
    public FloatingActionButton enviar;
    public SharedPreferences sharp;
    public static String longitudeSalva;
    public static String latitudeSalva;
    public static ArrayList<Pontos> listaitens = new ArrayList<>();
    public Double latitude = Double.valueOf(latitudeSalva);
    public Double longitude = Double.valueOf(longitudeSalva);
    public int estilo_mapa;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragm_mapa, container, false);
        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.viewMapa);
        mapFragment.getMapAsync(this);
        enviar = v.findViewById(R.id.botao_enviar_ir);
        return v;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        enviar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), ActivityEnviar.class));
            }
        });
    }
    @Override
    public void onMapReady(final GoogleMap googleMapas) {
        if (listaitens.size() == 0) {
           FragmentTransaction ft = getFragmentManager().beginTransaction();
          ft.detach(AbaMapa.this).attach(AbaMapa.this).commit();
           }

           verificarHorario();

        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(latitude, longitude))
                .bearing(45)
                .tilt(20)
                .build();
        CameraUpdate cU = CameraUpdateFactory.newCameraPosition(cameraPosition);
        googleMapas.animateCamera(cU);
        googleMapas.setMinZoomPreference(13f);

        for (int i = 0; i < listaitens.size(); i++) {
            Pontos item = listaitens.get(i);
            googleMapas.addMarker(new com.google.android.gms.maps.model.MarkerOptions()
                    .position(new LatLng(Double.valueOf(item.chamaLatitude()), Double.valueOf(item.chamaLongitude())))
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ponto))
                    .title(item.chamaNome()))
                    .setTag(String.valueOf(i));
        }
            googleMapas.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marcador) {
                Intent intent = new Intent(getActivity(), ActivityDetalhes.class);
                int aa = Integer.valueOf(marcador.getTag().toString());
                ActivityDetalhes.Info.id = marcador.getId();
                AbaInfos.nome = marcador.getTitle();
                AbaInfos.id = marcador.getId();
                ActivityDetalhes.Info.imagem = listaitens.get(aa).chamaImagem();
                AbaInfos.datahora = listaitens.get(aa).chamaDataHora();
                ActivityImagem.imagem = listaitens.get(aa).chamaImagem();
                AbaInfos.idUsuario = listaitens.get(aa).chamaIdUsuario();
                AbaInfos.nome_pessoa = listaitens.get(aa).chamaNomePessoa();
                AbaInfos.testeid = marcador.getId();
                ActivityPrincipal.idUsuario = listaitens.get(aa).chamaIdUsuario();
                ActivityApagar.idEnvio = listaitens.get(aa).chamaCodigoEnvio();
                for (int i = 0;i<24;i++ ){
                    if (marcador.getId().equals("m"+i)){ ActivityDetalhes.Info.num=i;AbaInfos.n =i;}
                }
                AbaMapa.this.startActivity(intent);
            }
        });
        MapStyleOptions estiloMapa = MapStyleOptions.loadRawResourceStyle(this.getContext(), estilo_mapa);
        googleMapas.setMapStyle(estiloMapa);

    }

    private void verificarHorario() {
        Calendar cal = Calendar.getInstance();
        int hora = cal.get(Calendar.HOUR_OF_DAY);
        if(hora < 6 || hora > 20){
            estilo_mapa = R.raw.estilo_mapa_noite;
        } else {
            estilo_mapa = R.raw.estilo_mapa_dia;
        }
    }

    }